package com.example.flowlayoutdemo;

import android.content.Context;
import android.os.Build;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.RequiresApi;

public class FlowLayout extends ViewGroup {
    public FlowLayout(Context context) {
        super(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);


        int lineWidth = 0; // 行宽
        int lineHeight = 0; // 行高
        int height = 0; // 整个FlowLayout所占的高度
        int width = 0; // 整个Flowlayout所占用的高度

        int childNumber = getChildCount();
        for (int i = 0; i < childNumber; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);

            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            // getMeasuredWidth()在measure结束以后就可以得到宽度值，getWidth()需要等到layout完成
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

            if (lineWidth + childWidth > measureWidth) {
                // 需要换行
                width = Math.max(lineWidth, width);
                height += childHeight;

                // 当前行放不下该控件，换行后更新行宽和行高
                lineHeight = childHeight;
                lineWidth = childWidth;
            } else {
                // 累加lineWidth,lineHeight取最大值
                lineHeight = Math.max(lineHeight, childHeight);
                lineWidth += childWidth;
            }

            if (i == childNumber - 1) {
                height += lineHeight;
                width = Math.max(width, lineWidth);
            }
            setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY ? measureWidth : width),
                    (measureHeight == MeasureSpec.EXACTLY ? measureHeight : height));
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 布局所有的子控件
        int childNumber = getChildCount();
        int lineWidth = 0;
        int lineHeight = 0;
        int top = 0;
        int left = 0;
        for (int index = 0; index < childNumber; index++) {
            View child = getChildAt(index);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            if (childWidth + lineWidth > getMeasuredWidth()) {

                // 换行放置的化需要更新top和left的值
                top += lineHeight;
                left = 0;

                lineHeight = childHeight;
                lineWidth = childWidth;
            } else {
                lineHeight = Math.max(lineHeight, childHeight);
                lineWidth += childWidth;
            }

            // 计算空间的left,top,right,bottom的值
            int leftPosition = left + lp.leftMargin;
            int topPosition = top + lp.topMargin;
            int rightPosition = leftPosition + child.getMeasuredWidth();
            int bottomPosition = topPosition + child.getMeasuredHeight();
            child.layout(leftPosition, topPosition, rightPosition, bottomPosition);
            left += childWidth;
        }
    }
}
